package com.jyw.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyw.system.domain.dto.UserTeamAddDto;
import com.jyw.system.domain.dto.UserTeamPageDto;
import com.jyw.system.domain.vo.UserTeamPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jyw.system.mapper.SysUserTeamMapper;
import com.jyw.system.domain.SysUserTeam;
import com.jyw.system.service.ISysUserTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 用户与小组关系Service业务层处理
 * 
 * @author system
 * @date 2023-08-07
 */
@Service
public class SysUserTeamServiceImpl extends ServiceImpl<SysUserTeamMapper,SysUserTeam> implements ISysUserTeamService
{
    @Resource
    private SysUserTeamMapper sysUserTeamMapper;

    /**
     * 查询用户与小组关系
     * 
     * @param id 用户与小组关系主键
     * @return 用户与小组关系
     */
    @Override
    public SysUserTeam selectSysUserTeamById(Long id)
    {
        return sysUserTeamMapper.selectSysUserTeamById(id);
    }

    /**
     * 查询用户与小组关系列表
     * 
     * @param sysUserTeam 用户与小组关系
     * @return 用户与小组关系
     */
    @Override
    public List<SysUserTeam> selectSysUserTeamList(SysUserTeam sysUserTeam)
    {
        return sysUserTeamMapper.selectSysUserTeamList(sysUserTeam);
    }

    /**
     * 新增用户与小组关系
     * 
     * @param sysUserTeam 用户与小组关系
     * @return 结果
     */
    @Override
    public int insertSysUserTeam(SysUserTeam sysUserTeam)
    {
        return sysUserTeamMapper.insertSysUserTeam(sysUserTeam);
    }

    /**
     * 修改用户与小组关系
     * 
     * @param sysUserTeam 用户与小组关系
     * @return 结果
     */
    @Override
    public int updateSysUserTeam(SysUserTeam sysUserTeam)
    {
        return sysUserTeamMapper.updateSysUserTeam(sysUserTeam);
    }

    /**
     * 批量删除用户与小组关系
     * 
     * @param ids 需要删除的用户与小组关系主键
     * @return 结果
     */
    @Override
    public int deleteSysUserTeamByIds(Long[] ids)
    {
        return sysUserTeamMapper.deleteSysUserTeamByIds(ids);
    }

    /**
     * 删除用户与小组关系信息
     * 
     * @param id 用户与小组关系主键
     * @return 结果
     */
    @Override
    public int deleteSysUserTeamById(Long id)
    {
        return sysUserTeamMapper.deleteSysUserTeamById(id);
    }

    @Override
    public int addSysUsetTeam(UserTeamAddDto userTeamAddDto) {
        return userTeamAddDto.getUserIdList().stream().mapToInt(data -> {
            //判断当前用户是否存在与该小组
            if (!userIsInTeam(userTeamAddDto.getTeamId(), data)) {
                //不存在新增
                SysUserTeam userTeam = userTeamAddDto.buildUserTeam(data);
                return sysUserTeamMapper.insertSysUserTeam(userTeam);
            }
            return 0;
        }).sum();
    }

    @Override
    public List<UserTeamPageVo> selectSysUserTeamPage(UserTeamPageDto userTeamPageVo) {
        List<UserTeamPageVo> list = sysUserTeamMapper.selectSysUserTeamPage(userTeamPageVo);
        return list;
    }

    @Override
    public List<UserTeamPageVo> getUserNotSelectList(UserTeamPageDto userTeamPageVo) {
        return sysUserTeamMapper.getUserNotSelectList(userTeamPageVo);
    }

    @Override
    public List<Long> getTeamIdByUser(Long userId) {
        return lambdaQuery().eq(SysUserTeam::getUserId, userId)
                .eq(SysUserTeam::getIsEnable, 1).list().stream().map(SysUserTeam::getTeamId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<Long> teamLeaderByUserId(Long userId) {
        //查询出小组信息
        List<SysUserTeam> list = lambdaQuery().eq(SysUserTeam::getUserId, userId)
                .eq(SysUserTeam::getIsLeader, 1)
                .eq(SysUserTeam::getIsEnable, 1)
                .list();
        return list.stream().map(SysUserTeam::getTeamId).distinct().collect(Collectors.toList());
    }

    @Override
    public Long getTeamLeader(Long teamId) {
        return lambdaQuery()
                .eq(SysUserTeam::getTeamId, teamId)
                .eq(SysUserTeam::getIsLeader, 1)
                .one().getUserId();
    }

    @Override
    public List<Long> getTeamLeaderByMark(Long makeUser) {
        return sysUserTeamMapper.getTeamLeaderByMark(makeUser);
    }

    private boolean userIsInTeam(Long teamId,Long userId){
        return sysUserTeamMapper.selectCount(new QueryWrapper<SysUserTeam>()
                .eq(StrUtil.toUnderlineCase(SysUserTeam.Fields.teamId), teamId)
                .eq(StrUtil.toUnderlineCase(SysUserTeam.Fields.userId) , userId)).intValue() > 0 ? true : false;
    }
}
